﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Quartz;
using Quartz.Impl;

namespace XsqTech.SmartOA.Application.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        [HttpGet("TestQuartz")]
        public async Task<IActionResult> TestQuartz()
        {
            //1.创建一个触发器
            var schedulerFactory = new StdSchedulerFactory();
            var scheduler = await schedulerFactory.GetScheduler();
            await scheduler.Start();
            Console.WriteLine($"任务调度器已开启");

            //创建作业和触发器
            var jobDetail = JobBuilder.Create<FirstJob>().Build();
            //var trigger = TriggerBuilder.Create()
            //                            .WithSimpleSchedule(m => {
            //                                m.WithRepeatCount(3)//循环次数
            //                                .WithIntervalInSeconds(1);//间隔时间
            //                            })
            //                            .Build();
            var trigger = TriggerBuilder.Create()
                            .WithCronSchedule("* * * * * ? *")
                            .Build();

            //添加调度
            await scheduler.ScheduleJob(jobDetail, trigger);

            return Ok();
            //2.创建一个工作任务
            //3.创建一个触发器（简单的、CRON触发器）
        }
    }

    //新建一个Job的实现类
    public class FirstJob : IJob
    {
        public Task Execute(IJobExecutionContext context)
        {
            return Task.Factory.StartNew(() =>
            {
                System.Console.WriteLine("1111");
            });
            
        }
    }

}
